﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using BLL;
using Business;
using GodHelper.Models;
using System.Web.Security;
using GodHelper.Util;
namespace GodHelper.Controllers
{
    /// <summary>
    /// 登录  注册  注销
    /// </summary>
    public class Mange_StateController : Controller
    {
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="Email"></param>
        /// <param name="Psw"></param>
        /// <param name="AutoLogin"></param>
        /// <returns></returns>
        public ActionResult Login(string Email, string Psw, bool AutoLogin)
        {
            int result = Business.StateMange.Login(Email, Psw);
            if (result == -1)  //用户名不存在
            {
                return Json(new { Result = false, Message = "用户名不存在" });
            }
            else if (result == 0) //密码错误
            {
                return Json(new { Result = false, Message = "密码错误" });
            }
            else if (result == 1) //登录成功
            {
                if (AutoLogin)   //保存密码
                {
                    Session["AutoLogin"] = true;

                    Response.Cookies["AutoLogin"].Value = "true";
                    //DateTime dtAutoLogin = DateTime.Now;
                    //dtAutoLogin.AddDays(7);
                    //Response.Cookies["AutoLogin"].Expires = dtAutoLogin;
                }
                Session["Useremail"] = Email;

                Response.Cookies["Useremail"].Value = Email;
                //DateTime dtUseremail = DateTime.Now;
                //dtUseremail.AddDays(7);
                //Response.Cookies["Useremail"].Expires = dtUseremail;



                return Json(new { Result = true, Message = "登录成功" });
            }
            return Json(new { Result = false, Message = "登录失败" });
        }

        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="Email"></param>
        /// <param name="Psw"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Register(String Email, String Psw)
        {
            if (Business.StateMange.Register(Email, Psw))   //注册成功
            {
                return Json(new { Result = true, Message = "注册成功" });
            }
            return Json(new { Result = false, Message = "邮箱已被使用，注册失败！" });
    
        }


        /// <summary>
        /// 注销
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Log_Off()
        {
            FormsAuthentication.SignOut();
            if (Session["Useremail"] != null)
            {
                string key = Session["Useremail"].ToString();
                UserModelManager.instance.remove(key);
            }

            DateTime dtUseremail = DateTime.Now;
            dtUseremail.AddDays(-1);
            Response.Cookies["Useremail"].Expires = dtUseremail;
            Response.Cookies["AutoLogin"].Expires = dtUseremail;
            Session.RemoveAll();
            
            return Json(new { Result = true });
        }
    }
}
